package com.chalk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.common.util.IdGenerator;
import com.chalk.common.util.Result;
import com.chalk.dao.TicketOrderDao;
import com.chalk.exception.ColumnNotEmptyException;
import com.chalk.model.Member;
import com.chalk.model.Ticket;
import com.chalk.model.TicketOrder;
import com.chalk.model.TicketOrderItem;
import com.chalk.service.MemberService;
import com.chalk.service.TicketOrderItemService;
import com.chalk.service.TicketOrderService;
import com.chalk.service.TicketService;

import com.chalk.util.MapUtil;
import com.chalk.util.MpUtil;
import com.chalk.util.WxUtil;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-24
 */

@Service
public class TicketOrderServiceImpl extends ServiceImpl<TicketOrderDao, TicketOrder> implements TicketOrderService {
    //localdatetime定义格式
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Autowired
    MpUtil<TicketOrder> mpUtil;
    @Autowired
    TicketOrderItemService ticketOrderItemService;
    @Autowired
    MemberService memberService;

    @Override
    public int addTicketOrder(TicketOrder entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateTicketOrder(TicketOrder entity) {
        return baseMapper.updateById(entity);
    }


    @Override
    public int deleteTicketOrder(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public Page<TicketOrder> getTicketOrderForPage(Map<String, Object> map) throws ColumnNotEmptyException {
        Page<TicketOrder> page = mpUtil.getPage(map);
        page.setDescs(mpUtil.getDesc("create_time"));
        Page<TicketOrder> ticketOrderPage = page.setRecords(this.baseMapper.getTicketOrderForPage(page,map));
        ticketOrderPage.getRecords().forEach(ticketOrder -> {
            ticketOrder.setTicketOrderItemList(ticketOrderItemService.getTicketOrderItems(ticketOrder.getTicketOrderId()));
        });
        return ticketOrderPage;
    }
}